package com.mdd.front.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.common.entity.order.Order;
import com.mdd.common.entity.user.User;
import com.mdd.common.entity.way.DevPayWay;
import com.mdd.common.enums.OrderStatusEnum;
import com.mdd.common.enums.PaymentEnum;
import com.mdd.common.mapper.order.OrderMapper;
import com.mdd.common.mapper.user.UserMapper;
import com.mdd.common.mapper.way.DevPayWayMapper;
import com.mdd.common.utils.ConfigUtil;
import com.mdd.common.utils.StringUtil;
import com.mdd.common.utils.TimeUtil;
import com.mdd.common.utils.UrlUtil;
import com.mdd.front.LikeFrontThreadLocal;
import com.mdd.front.service.system.ISystemDevPayWayService;
import com.mdd.front.vo.system.SystemDevPayWayDetailVo;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 支付方式实现类
 */
@Service
public class SystemDevPayWayServiceImpl implements ISystemDevPayWayService {
        
    @Resource
    DevPayWayMapper devPayWayMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private OrderMapper orderMapper;

    /**
     * 支付方式列表
     *
     * @author panweiliang
     * @return List<SystemDevPayWayListVo>
     */
    @Override
    public Map<String, Object> list(Integer scene, Integer orderId, String type) {
        MPJQueryWrapper<DevPayWay> mpjQueryWrapper = new MPJQueryWrapper<DevPayWay>()
                .selectAll(DevPayWay.class)
                .select("pay.name as payName, pay.image as payImage, pay.pay_way as payWay")
                .innerJoin("la_dev_pay pay ON pay.id=t.pay_id")
                .eq("t.status", "1")
                .eq("t.scene", scene)
                .orderByDesc(Arrays.asList("t.id"));
        if ("recharge".equals(type)){
            mpjQueryWrapper.nested( wq -> wq.ne("pay_id",2));
        }
        List<SystemDevPayWayDetailVo> list = devPayWayMapper.selectJoinList(SystemDevPayWayDetailVo.class, mpjQueryWrapper);
        //场景:1-微信小程序;2-微信公众号;3-H5;4-PC商城;5-APP;
        list.forEach(item -> {
            item.setPayImage(UrlUtil.toAbsoluteUrl(item.getPayImage()));
        });

        Map<String, Object> map = new LinkedHashMap<>();
        map.put("payWayList", list);

        // 如果是充值订单直接返回支付方式
        if("recharge".equals(type)){
            return map;
        }

        // 获取用户余额信息
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(User::getMoney);
        lambdaQueryWrapper.eq(User::getId, LikeFrontThreadLocal.getUserId());
        lambdaQueryWrapper.eq(User::getIsDelete,0);
        lambdaQueryWrapper.last("limit 1");
        User user = userMapper.selectOne(lambdaQueryWrapper);
        //查询预约订单
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderLambdaQueryWrapper.select(Order::getCancelTime);
        orderLambdaQueryWrapper.eq(Order::getId,orderId);
        orderLambdaQueryWrapper.eq(Order::getPayStatus, PaymentEnum.UN_PAID.getCode());
//        orderLambdaQueryWrapper.eq(Order::getOrderStatus, OrderStatusEnum.WAIT_PAY.getStatus());
        orderLambdaQueryWrapper.eq(Order::getIsDelete,0);
        orderLambdaQueryWrapper.last("limit 1");
        Order order = orderMapper.selectOne(orderLambdaQueryWrapper);
        Assert.notNull(order, "此订单未处于待支付状态！");

        // 预约订单返回余额支付和过期时间
        map.put("money",user.getMoney());
        map.put("cancelTime", order.getCancelTime());
        return map;
    }
}
